package cn.lx.es.query.processor;

import cn.lx.es.query.annotation.RangeSearch;
import cn.lx.es.query.search.entity.Range;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.lang.reflect.Field;

/**
 * @author wcy
 * @version 1.0
 * @description: 内置处理器 - 范围搜索处理器
 * @date 2025-07-02 下午1:22
 */
public class SearchRangeProcessor implements SearchAnnotationProcessor<RangeSearch> {
    @Override
    public void process(RangeSearch annotation, Field field, Object value, BoolQueryBuilder boolQueryBuilder) {

        if (value instanceof String && value.toString().contains(",")) {
            String range = (String) value;
            String[] split = range.split(",");
            boolQueryBuilder.must(QueryBuilders.rangeQuery(field.getName())
                    .from(split[0]).to(split[1]));
        }
    }
    @Override
    public Class<RangeSearch> getAnnotationType() {
        return RangeSearch.class;
    }

}
